﻿#include "usermanage.h"
#include "ui_usermanage.h"
#include <QHeaderView>
#include <QAbstractItemView>
#include <QMessageBox>
#include "globalvars.h"
#include "execsql.h"
#include <QDebug>

UserManage::UserManage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserManage)
{
    ui->setupUi(this);
    m_page = 1;
    m_change = false;

    updateUserInfoShow();
    initWindowsUnits();
}

UserManage::~UserManage()
{
    delete ui;
}

void UserManage::initWindowsUnits()
{
    ui->le_condition->setEnabled(false);
    ui->pb_save->setEnabled(false);

    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不能对表格内容进行修改
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}
void UserManage::updateUserInfo()
{
    ExecSQL::searchAllUserInfos();
    updateUserInfoShow();
}
void UserManage::updateUserInfoShow()
{
    //更新页数，更新内容
    int size = GlobalVars::g_userInfoList->size();
    int sizeTemp = size;

    m_pageCount = 1;
    qDebug() << "size = " << sizeTemp;
    while (sizeTemp > 10)
    {
        m_pageCount += (sizeTemp / 10);
        sizeTemp /= 10;
    }

    if (m_page > m_pageCount)
    {
        m_page = m_pageCount;
    }
    ui->tableWidget->clearContents();
    ui->lb_pageShow->setText(QString::number(m_page) + "/" + QString::number(m_pageCount));

    int index = (m_page-1) * 10;
    if (size >= m_page * 10)
    {
        size = m_page * 10;
    }
    else
    {
        size %= 10;
        size += index;
    }

    QString role;
    for(int i = 0; index < size; index++,i++)
    {
        if (GlobalVars::g_userInfoList->at(index).getRole() == "1")
        {
            role = "商家";
        }else
        {
            role = "用户";
        }

        QTableWidgetItem *item = new QTableWidgetItem(GlobalVars::g_userInfoList->at(index).getNumber());
        ui->tableWidget->setItem(i, 0, item);
        item = new QTableWidgetItem(GlobalVars::g_userInfoList->at(index).getName());
        ui->tableWidget->setItem(i, 1, item);
        item = new QTableWidgetItem(role);
        ui->tableWidget->setItem(i, 2 ,item);
        item = new QTableWidgetItem(GlobalVars::g_userInfoList->at(index).getCertificate());
        ui->tableWidget->setItem(i, 3, item);
        item = new QTableWidgetItem(GlobalVars::g_userInfoList->at(index).getRealName());
        ui->tableWidget->setItem(i, 4, item);

        item = new QTableWidgetItem(GlobalVars::g_userInfoList->at(index).getSex());
        ui->tableWidget->setItem(i, 5, item);
        item = new QTableWidgetItem(GlobalVars::g_userInfoList->at(index).getBirthday());
        ui->tableWidget->setItem(i, 6, item);
        item = new QTableWidgetItem(GlobalVars::g_userInfoList->at(index).getPhone());
        ui->tableWidget->setItem(i, 7, item);
        item = new QTableWidgetItem(GlobalVars::g_userInfoList->at(index).getEmail());
        ui->tableWidget->setItem(i, 8, item);
        item = new QTableWidgetItem(GlobalVars::g_userInfoList->at(index).getCertPath());
        ui->tableWidget->setItem(i, 9, item);
    }
}

void UserManage::on_pb_nextPage_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    if (m_page >= m_pageCount)
    {
        return;
    }
    m_page++;
    updateUserInfoShow();
}
void UserManage::on_pb_lastPage_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    if(m_page <= 1)
    {
        return;
    }
    m_page--;
    updateUserInfoShow();
}
void UserManage::on_pb_firstPage_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    if(m_page == 1)
    {
        return;
    }
    m_page = 1;
    updateUserInfoShow();
}
void UserManage::on_pb_endPage_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    if(m_page == m_pageCount)
    {
        return;
    }
    m_page = m_pageCount;
    updateUserInfoShow();
}
void UserManage::on_pb_jump_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    QString text = ui->le_jumpPage->text();
    int pageCount = text.toInt();
    if  (text ==  "")
    {
        QMessageBox::warning(this,
                                     "警告",
                                     "请输入页数",
                                     QMessageBox::Yes,
                                     QMessageBox::Yes);
        return;
    }
    if (pageCount < 1 || pageCount > m_pageCount)
    {
        QMessageBox::warning(this,
                                     "警告",
                                     "请输入正确的页数范围",
                                     QMessageBox::Yes,
                                     QMessageBox::Yes);
        return;
    }
    if (pageCount == m_page)
    {
        return;
    }
    m_page = pageCount;
    updateUserInfoShow();
}

void UserManage::on_pb_search_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    QString condition = ui->le_condition->text();
    int index = ui->cb_role->currentIndex();
    if (index)//选择商家或者用户
    {
        QString role = QString::number(index-1);
        switch (ui->cb_select->currentIndex())
        {
            case SEL_NONE:
                ExecSQL::searchAllUserInfosForRole(role);
                break;
            case SEL_NUMBER:
                ExecSQL::searchUserInfoForIDAndRole(condition, role);
                break;
            case SEL_NAME:
                ExecSQL::searchUserInfoForNameAndRole(condition, role);
                break;
            case SEL_SEX:
                ExecSQL::searchUserInfoForSexAndRole(condition, role);
                break;
            case SEL_REAL_NAME:
                ExecSQL::searchUserInfoForRealNameAndRole(condition, role);
                break;
            case SEL_PHONE:
                ExecSQL::searchUserInfoForPhoneAndRole(condition, role);
                break;
            case SEL_EMAIL:
                ExecSQL::searchUserInfoForEmailAndRole(condition, role);
                break;
        }
    }
    else
    {
        switch (ui->cb_select->currentIndex())
        {
            case SEL_NONE:
                ExecSQL::searchAllUserInfos();
                break;
            case SEL_NUMBER:
                ExecSQL::searchUserInfoForID(condition);
                break;
            case SEL_NAME:
                ExecSQL::searchUserInfoForName(condition);
                break;
            case SEL_SEX:
                ExecSQL::searchUserInfoForSex(condition);
                break;
            case SEL_REAL_NAME:
                ExecSQL::searchUserInfoForRealName(condition);
                break;
            case SEL_PHONE:
                ExecSQL::searchUserInfoForPhone(condition);
                break;
            case SEL_EMAIL:
                ExecSQL::searchUserInfoForEmail(condition);
                break;
        }
    }

    updateUserInfoShow();
}
void UserManage::on_pb_modify_clicked()
{
    m_change = true;
    ui->pb_save->setEnabled(true);
    ui->pb_modify->setEnabled(false);
    ui->tableWidget->setEditTriggers(QAbstractItemView::DoubleClicked); //不能对表格内容进行修改
}
void UserManage::on_pb_save_clicked()
{
    m_change = false;
    ui->pb_save->setEnabled(false);
    ui->pb_modify->setEnabled(true);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不能对表格内容进行修改

    //修改数据库/刷新
    for(auto iter : m_modifyList)
    {
        int index = iter.at(1).toInt();
        switch (index)
        {
            case USER_CERT:
                ExecSQL::modifyUserInfoForCertState(iter.at(0),iter.at(2));
                break;
            case USER_NAME:
                ExecSQL::modifyUserInfoForName(iter.at(0),iter.at(2));
                break;
            case USER_ROLE:
                ExecSQL::modifyUserInfoForRole(iter.at(0),iter.at(2));
                break;
            case USER_REAL_NAME:
                ExecSQL::modifyUserInfoForRealName(iter.at(0),iter.at(2));
                break;
            case USER_SEX:
                ExecSQL::modifyUserInfoForSex(iter.at(0),iter.at(2));
                break;

            case USER_BIRTH:
                ExecSQL::modifyUserInfoForBirthday(iter.at(0),iter.at(2));
                break;
            case USER_PHONE:
                ExecSQL::modifyUserInfoForPhone(iter.at(0),iter.at(2));
                break;
            case USER_EMAIL:
                ExecSQL::modifyUserInfoForEmail(iter.at(0),iter.at(2));
                break;
            default: break;
        }
    }
    on_pb_search_clicked();
}
void UserManage::on_pb_delete_clicked()
{
    QItemSelectionModel *selections = ui->tableWidget->selectionModel(); //返回当前的选择模式
    QModelIndexList selectedsList = selections->selectedRows(); //返回所有选定的模型项目所在行的列表
    if (selectedsList.size() < 1)
    {
        QMessageBox::warning(this,
                                 "错误",
                                 "请选择要删除的行",
                                 QMessageBox::Ok,
                                 QMessageBox::Ok);
        return;
    }

    int result = QMessageBox::warning(this,
                         "警告",
                         "是否确定删除此用户信息！！！",
                         QMessageBox::Yes | QMessageBox::Cancel,
                         QMessageBox::Cancel);

    if (result == QMessageBox::Yes)
    {
        for (int i = 0; i < selectedsList.count(); i++)//数据库进行删除
        {
            int row = selectedsList.at(i).row();
            ExecSQL::deleteUserInfo(ui->tableWidget->item(row,0)->text());
        }

        on_pb_search_clicked();
    }
}

void UserManage::on_cb_select_currentIndexChanged(int index)
{
    m_page = 1;
    if (index == 0)
    {
        ui->le_condition->setText("");
        ui->le_condition->setEnabled(false);
    }
    else
    {
        ui->le_condition->setEnabled(true);
    }
}
void UserManage::on_cb_role_currentIndexChanged(int index)
{
    m_page = 1;
}
void UserManage::on_tableWidget_cellChanged(int row, int column)
{
    if (!m_change)
    {
        return;
    }
    if (column == 0)
    {
        QMessageBox::warning(this,
                                     "警告",
                                     "不能修改用户名！！！",
                                     QMessageBox::Yes,
                                     QMessageBox::Yes);
        on_pb_save_clicked();
        updateUserInfoShow();
        return;
    }

    QString item = ui->tableWidget->item(row,column)->text();
    if (column == 2)
    {
        if (item == "商家")
        {
            item = "1";
        }else if (item== "用户")
        {
            item = "0";
        }else
        {
            return;
        }
    }

    QVector<QString> modify;
    modify.push_back(ui->tableWidget->item(row,0)->text());
    modify.push_back(QString::number(column));
    modify.push_back(item);
    m_modifyList.push_back(modify);

    qDebug() << "on_tableWidget_cellChanged(int row, int column)";
    qDebug() << "modify = " << modify;
    qDebug() << "m_modifyList = "  << m_modifyList;
}


